package dk.aau.walkietalkieui;

import java.util.prefs.Preferences;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.widget.Toast;

public class SettingsActivity extends PreferenceActivity implements
		OnSharedPreferenceChangeListener {

	public static final String KEY_PREF_SYNC_CONN = "prefConnectionStatus";
	public static final String KEY_PREF_SYNC_CONN_TYPE = "prefConnectionType";
	public static final String KEY_PREF_SYNC_NAME = "prefName";
	private String MYPREF = "MyPref";
	private String KEY_DEVICE_NAME = "prefUsername";
	private WifiManager wifi;
	private String deviceName;

	@SuppressWarnings("deprecation")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		addPreferencesFromResource(R.xml.settings);
		setTitleColor(getResources().getColor(R.color.white));

		// Manage the wifi connexion

		wifi = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
		SharedPreferences sp = PreferenceManager
				.getDefaultSharedPreferences(getApplicationContext());
		SharedPreferences.Editor editor = sp.edit();

		if (wifi.isWifiEnabled()) {
			editor.putBoolean(KEY_PREF_SYNC_CONN, true);
			editor.commit();
		} else {
			editor.putBoolean(KEY_PREF_SYNC_CONN, false);
			editor.commit();
		}
	}

	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
			String key) {
		if (key.equals(KEY_PREF_SYNC_CONN_TYPE)) {
			String txt = sharedPreferences.getString(KEY_PREF_SYNC_CONN_TYPE,
					"0");

			Toast.makeText(getApplicationContext(), R.string.connection_type + txt,
					Toast.LENGTH_LONG).show();
		}
		if (key.equals(KEY_DEVICE_NAME)) {
			deviceName = sharedPreferences
					.getString(KEY_DEVICE_NAME, "unknown");
			// only letters, numbers, hyphen can be used for device name
			Pattern p = Pattern.compile("[^[A-Z][a-z][0-9][/_]]+",
					Pattern.CASE_INSENSITIVE);
			Matcher m = p.matcher(deviceName);
			boolean nameNOK = m.find();
			if (nameNOK) {
				Toast.makeText(
						getApplicationContext(),
						R.string.error_message_name,
						Toast.LENGTH_LONG).show();
				EditTextPreference pref = (EditTextPreference) findPreference(key);
				pref.setText("");
			} else {
				Toast.makeText(getApplicationContext(),
						R.string.your_device_name + deviceName, Toast.LENGTH_LONG)
						.show();

			}
		}
		if (key.equals(KEY_PREF_SYNC_CONN)) {
			Boolean isOnline = sharedPreferences.getBoolean(KEY_PREF_SYNC_CONN,
					true);

			if (isOnline) {
				Toast.makeText(getApplicationContext(),
						R.string.start_broadcast_sender,
						Toast.LENGTH_SHORT).show();
				WalkieTalkieApplication.getApplication().startBroadcast();
				WalkieTalkieApplication.getApplication().startFileReceiver();
				Toast.makeText(getApplicationContext(),
						R.string.started_broadcast_sender_and_file_receiver,
						Toast.LENGTH_SHORT).show();
			} else {
				Toast.makeText(getApplicationContext(),
						R.string.stopping_broadcast_sender_and_file_receiver_,
						Toast.LENGTH_SHORT).show();
				WalkieTalkieApplication.getApplication().stopBroadcast();
				WalkieTalkieApplication.getApplication().stopFileReceiver();
				Toast.makeText(getApplicationContext(),
						R.string.stopped_broadcast_sender_and_file_receiver,
						Toast.LENGTH_SHORT).show();
			}
		}
	}

	@SuppressWarnings("deprecation")
	@Override
	protected void onResume() {
		super.onResume();
		getPreferenceScreen().getSharedPreferences()
				.registerOnSharedPreferenceChangeListener(this);

//		if (!nameNOK) { // name contains characters other than letters, numbers
//			// and hyphens
//			SharedPreferences.Editor editor = getPreferenceScreen()
//					.getSharedPreferences().edit();
//			editor.putString(KEY_DEVICE_NAME, "");
//			editor.commit();
//		}

	}

	@SuppressWarnings("deprecation")
	@Override
	protected void onPause() {
		super.onPause();
		getPreferenceScreen().getSharedPreferences()
				.unregisterOnSharedPreferenceChangeListener(this);

	}
}
